package org.frxm.myshares;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Objects;
import java.util.Set;
import java.util.StringTokenizer;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
/*第三方登陆之QQ*/
public class LoginActivity extends AppCompatActivity
implements PlatformActionListener{

    private EditText et1,et2;
    private Handler handler;
    private ImageView iv1,iv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //初始化
        ShareSDK.initSDK(this);
        init();
    }

    private void init() {
        et1=(EditText)findViewById(R.id.et_l_1);
        et2=(EditText)findViewById(R.id.et_l_2);
        iv1=(ImageView)findViewById(R.id.iv_l1);
        iv2=(ImageView)findViewById(R.id.iv_l2);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                HashMap<String,Objects>map= (HashMap<String, Objects>) msg.obj;
                et1.setText(map.get("nickname").toString());
                Picasso.with(LoginActivity.this).load(map.get("figureurl_2").
                        toString()).into(iv1);
                Picasso.with(LoginActivity.this).load(map.get("figureurl_1").
                        toString()).into(iv2);
            }
        };
    }
    public void click(View v){
        //开启QQ登陆界面
        login();
    }
    private void login()
    {
        //获取对应的平台对象
        Platform pf=ShareSDK.getPlatform(QQ.NAME);
        pf.authorize();//调用授权
        //设置登陆结果的回调接口
        pf.setPlatformActionListener(this);
        pf.showUser(null);//显示用户信息
    }
    /*成功*/
    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        Set<String> keys=hashMap.keySet();
        for (String key:keys)
        {
            Log.e("TS", "onComplete: 键="+key+",值="+hashMap.get(key));
        }
        Message msg=handler.obtainMessage(0x110);
        msg.obj=hashMap;
        handler.sendMessage(msg);
    }
    /*错误*/
    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Log.e("TS", "onError: 有错" );
    }
    /*取消*/
    @Override
    public void onCancel(Platform platform, int i) {
        Log.e("TS", "onCancel: 取消" );
    }
}
